﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/**
 * 需要asset store下载 DOTween 插件
 * 
 * Rigidbody2D 组件需要将 Collision Detection 设为 Continuous，将 Constraints/Freeze Rotation Z 勾选
 */
public partial class PlayerController : MonoBehaviour
{

    [Header("移动")]
    public float runSpeed = 6;  // 奔跑速度
    public float accelerateTime = 0.09f;    // 加速时间
    public float decelerateTime = 0.09f;    // 减速时间
    public Vector2 inputRebound;    // 输入误差，当速度超过该误差值时才执行移动，推荐 x=0.2 y=0

    // 奔跑速度倍率
    private float runSpeedRate = 60f;
    // 由 Mathf.SmoothDamp 计算出的x轴速度
    private float currentVelocityX;

    private bool isMoving;
    private bool isFaceToRight = true;     // 是否面向右方

    [Header("跳跃")]
    public float jumpSpeed = 8f;
    public float fallMultiplier = 4f;
    public float lowJumpMultiplier = 2.5f;

    private bool isJumping;
    private bool isFalling;

    [Header("冲刺")]
    public float dashForce = 19f;     // 推力
    public float dragForce = 8f;     // 阻力
    public float dragDuration = 0.3f;

    private bool wasDashed;     // 是否已冲刺过
    private bool isDashing;

    [Header("地面判定")]
    public Vector2 centerPointOffset;       // 角色中心点的偏移量
    public Vector2 overlapBoxSize;          // 检测盒子大小，盒子与地面接触的部分可以多增加一些，这样能够提供更宽松的起跳检测
    public LayerMask groundLayerMask;       // 地面的 LayerMask

    private bool isOnGround;

    

    private Rigidbody2D rb;
    private Animator anim;
    private SpriteRenderer sr;

    
    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        sr = GetComponent<SpriteRenderer>();
    }


    void FixedUpdate()
    {
        Movement();
        SwitchAnimation();
    }


    // 显示判定框
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube((Vector2)transform.position + centerPointOffset, overlapBoxSize);
    }
}
